package com.yanqu.road.entity.util;

import com.yanqu.road.entity.activity.game.config.StrengthConfig;

public class PowerHelper {

    /**
     * 更新体力
     *
     * @param currentStrength
     * @param strengthUpdateTime
     * @return
     */
    public static StrengthResult addPower(int currentStrength,//当前体力
                                          long strengthUpdateTime,//更新体力时间
                                          StrengthConfig sc
    ) {
        long time = System.currentTimeMillis();

        int betweenTime = sc.getSecends();//间隔秒数
        int addStrength = sc.getAddStrength();//增加体力数
        int maxStrength = sc.getMaxStrength();//最大值

        long strengthTime = time - strengthUpdateTime;
        //相差秒数
        long second = strengthTime / 1000;//上次更新时间和当前时间相差的秒数

        StrengthResult result = null;
        if (second > betweenTime) {
            //查看间隔秒数是周期的多少倍
            long mo = 1l;
            if (betweenTime != 0) {
                mo = second / betweenTime;
            }
            if (currentStrength + addStrength * mo > maxStrength) {
                if (currentStrength > maxStrength) {
                    result = addStrength(currentStrength, strengthUpdateTime, 0, time, betweenTime, maxStrength);
                } else {
                    int num = maxStrength - currentStrength;
                    result = addStrength(currentStrength, strengthUpdateTime, num, time, betweenTime, maxStrength);
                }
            } else {
                int num = addStrength * Integer.valueOf(String.valueOf(mo));
                result = addStrength(currentStrength, strengthUpdateTime, num, time, betweenTime, maxStrength);
            }
        }
        return result;
    }


    public static StrengthResult reduceStrength(int nowStrength, int num, StrengthConfig strengthConfig, long strengthTime) {
        StrengthResult result = new StrengthResult();

        long nowTime = System.currentTimeMillis();

        int newStrength = nowStrength - num;

        result.setStrength(newStrength);

        result.setLastUpdateTime(nowTime);

        result.setStrengthUpdateTime(strengthTime);

        int maxStrength = strengthConfig.getMaxStrength();

        if (nowStrength >= maxStrength && newStrength < maxStrength) {
            result.setStrengthUpdateTime(nowTime);
        }
        return result;
    }


    private static StrengthResult addStrength(int strength, long strengthUpdateTime, int num, long time, int betweenTime, int maxStrength) {

        StrengthResult strengthResult = new StrengthResult();

        long strengthTime = time - strengthUpdateTime;
        long second = strengthTime / 1000;//上次更新时间和当前时间相差的秒数
        long mo = 1l;


        if (betweenTime != 0) {
            mo = second / betweenTime;
        }

        int nowStrength = strength;

        int newStrength = nowStrength + num;

        strengthResult.setStrength(newStrength);

        long oldRefreshTime = strengthUpdateTime;

        long addTime = betweenTime * 1000 * mo + oldRefreshTime;
        strengthResult.setStrengthUpdateTime(addTime);
        strengthResult.setLastUpdateTime(time);
        if (nowStrength >= maxStrength && newStrength < maxStrength) {
            strengthResult.setStrengthUpdateTime(time);
        }
        return strengthResult;
    }


}
